﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HighEnergyCrystalBullet : MissileFunction
{
    bool m_Boom = false;
    float m_BoomTimeCount = 0;
    public float Energy{
        set {
            m_Energy = value;
            m_HP = m_Energy / 100;
            Damage = m_Energy / 4;
            Life = m_Energy / 100;
            float scale = m_Energy / 1000;
            transform.localScale = new Vector3(scale, scale, scale);
            m_RotationSpeed = m_Energy / 2;
        }
        get { return m_Energy; }
    }
    float m_Energy = 0;
    float m_RotationSpeed = 10;
    public bool m_IsEmit = false;
    public HighEnergyCrystalBullet()
    {
    }
    public override void Start()
    {
        m_IsUpdate = false;
        base.Start();
    }

    public override void Update()
    {
        m_IsUpdate = m_IsEmit;
        base.Update();
        if (UtilsGame.IsOnline() && !UtilsGame.IsServer(gameObject))
        {
            return;
        }
        CurFlyVelocity = FlyVelocity;
        if (Death)
            return;
        transform.rotation *= Quaternion.AngleAxis(m_RotationSpeed * Time.deltaTime, new Vector3(0, 0, 1));

        if(m_Boom)
        {
            m_BoomTimeCount += Time.deltaTime;
            if (m_BoomTimeCount > 0.5f)
            {
                m_BoomTimeCount = 0;
                enabled = false;

                if (!BulletPool.get().Recycle(gameObject))
                {
                    Destroy(this.gameObject);
                }
                if (OnBoomCallback != null)
                    OnBoomCallback();

            }
        }
    }
    protected override void OnDestroy()
    {
        base.OnDestroy();
    }
    protected override void BoomEffect()
    {
        base.BoomEffect();
    }
    override protected void OnTriggerEnter2D(Collider2D collision)
    {
        if (UtilsGame.IsOnline() && !UtilsGame.IsServer(gameObject))
        {
            return;
        }
        if (!UtilsGame.IsOfShips(m_Ship.GetComponent<Ship>(), collision.gameObject))
        {
            return;
        }
        if (CalcDamage(collision, m_Ship, Damage, WeaponType))
        {
        }
        if(!m_Boom)
        {
            if (UtilsGame.IsOnline() && UtilsGame.IsServer(gameObject))
                RpcOnBoomEffect(transform.position);
            BoomEffect();
            GetComponent<SpriteRenderer>().enabled = false;
            transform.localScale *= 1.3f;
        }
        m_Boom = true;
    }
    protected override void OnBoom()
    {
        if (UtilsGame.IsOnline() && UtilsGame.IsServer(gameObject))
            RpcOnBoomEffect(transform.position);
        BoomEffect();
        if (!BulletPool.get().Recycle(gameObject))
        {
            Destroy(this.gameObject);
        }
        if (OnBoomCallback != null)
            OnBoomCallback();
    }
}
